#ifndef _HTTP_CONNECTIN_IMPL_H
#define _HTTP_CONNECTIN_IMPL_H

#include <curl/curl.h>
#include "ConnectionManager.h"

LIB_UTIL_NAMESPACE_BEGIN

class CURLConnectionManager : public ConnectionManager
{
public:
	CURLConnectionManager(void);
	virtual ~CURLConnectionManager(void);

	virtual bool Initialize();
	virtual bool Unitialize();
	virtual HttpConnection* OpenConnection(tConnectionData& data);
};

class CURLHttpConnection : public HttpConnection
{
	friend class CURLConnectionManager;
private:
	CURLHttpConnection(tConnectionData& data);
	virtual ~CURLHttpConnection(void);

public:
	virtual bool Open();
	virtual bool Close();
	virtual bool Destroy();
	virtual bool Connect();
	virtual void Cancel();
	virtual void SetEventHandler(ConnectionEventHandler* handler);

private:
	void ReleaseBuffer();
	bool PerformConnection();

#ifdef _WINDOWS
	static int setsockoptcallback(void* clientp, curl_socket_t curlfd, curlsocktype purpose);
#endif 
	static int setsslctxcallback(CURL* curl, void* sslctx, void* parm);
	static size_t write(void* ptr, size_t size, size_t nmemb, void* stream);
	static int curl_progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);
private:
	tConnectionData& m_data;
	void* m_curlContext;
	ConnectionEventHandler* m_eventHandler;
	tDataBuffer* m_buffer;
	bool m_ret;
};

LIB_UTIL_NAMESPACE_END

#endif //_HTTP_CONNECTIN_IMPL_H

